package com.sunlands.qdcy.tongzhuo.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.sunlands.qdcy.tongzhuo.common.interceptor.SignInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <p>webmvc配置</p>
 *
 * @package: com.sunlands.zlcx.mpmsgservice.config
 * @date Created in 2017/11/30 15:41
 * @copyright: Copyright (c) founders
 */
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

    @Autowired
    SignInterceptor signInterceptor;

    @Bean
    ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        return objectMapper;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(signInterceptor).addPathPatterns("/**");
        super.addInterceptors(registry);
    }


//    @Override
//    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
//        //设置系统默认线程池
//        configurer.setTaskExecutor(defaultExecutor());
//    }


//    private ThreadPoolTaskExecutor getThreadPoolTaskExecutor(int corePoolSize, int maxPoolSize, String threadNamePrefix, boolean waitForTasksToCompleteOnShutdown, int awaitTerminationSeconds) {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(corePoolSize);
//        executor.setMaxPoolSize(maxPoolSize);
//        executor.setThreadNamePrefix(threadNamePrefix);
////        executor.setQueueCapacity(4096);
//        //设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean，这样这些异步任务的销毁就会先于外部资源的销毁
//        executor.setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown);
//        //用来设置线程池中任务的等待时间，如果超过这个时候还没有销毁就强制销毁，以确保应用最后能够被关闭，而不是阻塞住
//        executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
//        return executor;
//    }


    public static final long TIMEOUT = 5 * 1000;

    public enum WebTimeOutEnum {
        /**
         * 下载路径
         */
        DOWN_ASSERTS_LIABILITY("/user/diary/upload/image", 20 * 1000),;

        private String serviceUrl;
        private long timeout;

        WebTimeOutEnum(String serviceUrl, long timeout) {
            this.serviceUrl = serviceUrl;
            this.timeout = timeout;
        }

        public static WebTimeOutEnum url(String url) {
            for (WebTimeOutEnum timeOutEnum : WebTimeOutEnum.values()) {
                if (timeOutEnum.getServiceUrl().equals(url)) {
                    return timeOutEnum;
                }
            }
            return null;
        }

        public String getServiceUrl() {
            return serviceUrl;
        }

        public long getTimeout() {
            return timeout;
        }
    }
}
